package com.nick.sscplan;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.util.Log;

import com.nick.sscplan.bean.SscInfo;
import com.nick.sscplan.provider.SscDAO;
import com.nick.sscplan.util.Ssc_XmlHandler;

public class SscData {

    private static final String TAG = "ssc_plan";
    private SscInfo sscInfo = null;
    private List<SscInfo> sscInfos = null;

    public SscData() {
        // TODO Auto-generated constructor stub
    }

    public List<SscInfo> initData(URL url) {
        try {
            Log.d(TAG, "initData----->start");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            Log.d(TAG, "connection:" + connection);
            connection.setDoInput(true);
            connection.setConnectTimeout(1000*6);
            connection.connect();
            InputStream is = connection.getInputStream();
            BufferedInputStream stream = new BufferedInputStream(is);

            SAXParserFactory factory = SAXParserFactory.newInstance();
            XMLReader xmlReader = factory.newSAXParser().getXMLReader();
            Ssc_XmlHandler xmlHandle = new Ssc_XmlHandler();
            xmlReader.setContentHandler(xmlHandle); 
            xmlReader.parse(new InputSource(
                    new InputStreamReader(stream, "GBK")));

            sscInfos = xmlHandle.getSscInfo();
            Log.d(TAG, "sscInfos = " + sscInfos.size());

            stream.close();
            is.close();
        } catch (IOException e1) { 
            e1.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }

        Log.d(TAG, "initData----->end");
        return sscInfos;
    }
    

}
